﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Util.Log;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util;
using System.IO;
using PlanningGIS.Util.Serialization;
using System.Xml;
using PlanningGIS.MetaData;

namespace PlanningGIS.Product
{
    /// <summary>
    /// 从产品数据读取元数据信息相关设置
    /// </summary>
    public partial class FrmMetaInfoSet : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fd"></param>
        public FrmMetaInfoSet(List<MetaField> fd)
        {
            InitializeComponent();
            grdvwColName.DisplayMember = "Name";
            grdvwColName.ValueMember = "Name";
            grdvwColName.DataSource = fd;
        }

        /// <summary>
        /// 规则
        /// </summary>
        public List<ValueName> ReadRule { get; set; }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMetaInfoSet_Load(object sender, EventArgs e)
        {            
            SetRule(ReadRule);
        }

        /// <summary>
        /// 设置规则
        /// </summary>
        /// <param name="rules"></param>
        private void SetRule(List<ValueName> rules)
        {
            drgdvwRules.Rows.Clear();
            if (rules == null || rules.Count == 0) return;
            drgdvwRules.AllowUserToAddRows = false;
            foreach (ValueName v in rules)
            {
                int idx = drgdvwRules.Rows.Add();
                drgdvwRules.Rows[idx].Cells[grdvwColName.Index].Value = v.Name;
                drgdvwRules.Rows[idx].Cells[grdvwColFormat.Index].Value = v.Value;
            }
            drgdvwRules.AllowUserToAddRows = true;
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            ReadRule = GetRules();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 获取规则
        /// </summary>
        /// <returns></returns>
        private List<ValueName> GetRules()
        {
            if (drgdvwRules.Rows.Count == 0) return null;
            drgdvwRules.AllowUserToAddRows = false;
            List<ValueName> readRule = new List<ValueName>();
            foreach (DataGridViewRow r in drgdvwRules.Rows)
            {
                if (r.IsNewRow) continue;
                string value = r.Cells[grdvwColFormat.Index].Value.ToString();
                string name = r.Cells[grdvwColName.Index].Value.ToString();
                if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(value)) continue;
                readRule.Add(new ValueName() { Name = name, Value = value });
            }
            drgdvwRules.AllowUserToAddRows = true;
            return readRule;
        }

        /// <summary>
        /// 导入规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImpRule_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) != System.Windows.Forms.DialogResult.OK) return;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(openFileDialog1.FileName);
                List<ValueName> readRule = XmlSerialization.FromXml<List<ValueName>>(doc.OuterXml);
                SetRule(readRule);
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(this, "读取文件失败，可能不是标准的规则文件:" + ex.Message);
            }
        }

        /// <summary>
        /// 导出规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExpRule_Click(object sender, EventArgs e)
        {
            List<ValueName> readRule = GetRules();
            if (readRule == null || readRule.Count == 0) return;
            if (saveFileDialog1.ShowDialog(this) != System.Windows.Forms.DialogResult.OK) return;
            try
            {
                //string s = XmlSerialization.ToXml(readRule);
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(s);
                //doc.Save(saveFileDialog1.FileName);
                XmlSerialization.Serialize<List<ValueName>>(saveFileDialog1.FileName, readRule);
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(this, "规则保存失败:" + ex.Message);
            }            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
    }
}
